<?php
    $usmap = '/Users/huangyaohui/Desktop/allyes/newcar.svg';
    $im = new Imagick();
    $svg = file_get_contents($usmap);

    /*loop to color each state as needed, something like*/
    //  其实这块颜色替换没太大什么软用的
    $idColorArray = array(
        "AL" => "339966"
        ,"AK" => "0099FF"
        ,"WI" => "FF4B00"
        ,"WY" => "A3609B",
    );

    foreach($idColorArray as $state => $color){
    //Where $color is a RRGGBB hex value
        $svg = preg_replace(
            '/id="'.$state.'" style="fill:#([0-9a-f]{6})/'
            , 'id="'.$state.'" style="fill:#'.$color
            , $svg
        );
    }
    //设置个背景透明画布
    $im->setBackgroundColor(new ImagickPixel('transparent'));
    $im->readImageBlob($svg);

    /*png settings*/
    $im->setImageFormat("png24");
    $im->resizeImage(720, 445, imagick::FILTER_LANCZOS, 1);  /*Optional, if you need to resize*/

    $im->writeImage('/Users/huangyaohui/Desktop/allyes/newcar.png');
    $im->clear();
    $im->destroy();
